<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;

class IndexController extends Controller
{
    public function index(Request $req){
    	//$db = DB::table('films');
    	//打印电影类型显示到导航栏
        $ftype = DB::table('film_types')->get();
        //按地区查询类型显示到导航栏
        $farea = DB::table('areas')->get();
        //查询显示主页右上角内容
        $right_top = DB::table('films')->orderBy('fid','desc')->limit(6)->get();
        //查询为最新推荐的视频
        $recomend = DB::table('films')
        ->leftjoin('film_atts','films.fid','=','film_atts.fid')
        
        ->where('is_rec',1)
        ->limit(6)
        ->get();
        //查询最新上映电影
          $new = DB::table('films')
        ->leftjoin('film_atts','films.fid','=','film_atts.fid')
        
        ->orderBy('period','desc')
        ->limit(6)
        ->get();
        //查最受欢迎，HOT的影片
          $is_hot = DB::table('films')
        ->leftjoin('film_atts','films.fid','=','film_atts.fid')
        ->where('is_hot',1)
        ->orderBy('films.fid','desc')
        ->limit(6)
        ->get();
        
        //评论最多的影片
         $more_com = DB::table('films')
        ->leftjoin('film_atts','films.fid','=','film_atts.fid')
        ->orderBy('film_atts.is_pay','desc')
        ->limit(6)
        ->get();
        //最近更新的影片
          $recent = DB::table('films')
        ->leftjoin('film_atts','films.fid','=','film_atts.fid')
        ->orderBy('films.fid','desc')
        ->limit(6)
        ->get();
        //评分最高的排名
          $score = DB::table('films')
        ->leftjoin('film_atts','films.fid','=','film_atts.fid')
        ->orderBy('score','desc')
        ->limit(6)
        ->get();
        //底部轮播的或宣传的大片，放推荐度最高一的部
          $most_hot = DB::table('films')
        ->leftjoin('film_atts','films.fid','=','film_atts.fid')
        ->leftjoin('film_types','films.type_id','=','film_types.type_id')
       
        ->orderBy('is_rec','desc')
        ->limit(1)
        ->get();
        //print_r($farea);

        //登陆信息
          $user = $req->user();
        if ($user) {
            $name = $user->name;
        }else{   
            $name = 0;       
        }
        
        
        return view('Home/index',
        	
        	[
        	'right_top'=> $right_top,
        	'recomend' => $recomend,
        	'new'      => $new,
        	'is_hot'   => $is_hot,
        	'is_pay'  => $more_com,
        	'recent'   => $recent,
        	'score'    => $score,
        	'most_hot' => $most_hot,
            'name'     => $name
        	]);
    }
     
 	






 public function user(Request $req){

        $name = $req->user()->name;
        if ($name){
            $user = DB::table('users')->where('name',$name)->first();
            $data = ['user'=>$user];
            return view('Home/user',$data);
        }else{   
            $name = 0;   
            $data = ['name'=>$name];    
            return view('Home/index',$data);
        }
    }

    public function user_edit(Request $req){
        $name = $req->user()->name;
        if ($name){
            $user = DB::table('users')->where('name',$name)->first();
            $data = ['user'=>$user];
            return view('Home/user_edit',$data);
        }else{   
            $name = 0;   
            $data = ['name'=>$name];    
            return view('Home/index',$data);
        }
    }








}
